package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.itheima.pojo.Result.success;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    public Result list(){
        System.out.println("查询所有部门信息");
        List<Dept> deptlist = deptService.findAll();
        return success(deptlist);
    }

    @DeleteMapping("/depts")
    //前端请求参数和后端表格参数一致，则可以直接delete(Integer id)
    public Result delete(@RequestParam(value = "id", required  = false) Integer deptid){
        System.out.println("根据部门ID删除部门" + deptid );
        deptService.deleteById(deptid);
        return success();
    }

    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        System.out.println("新增部门" + dept);
        deptService.add(dept);
        return success();
    }

    @GetMapping("/depts/{id}")
    public Result getInfo(@PathVariable("id") Integer deptId){
        System.out.println("根据ID查询部门数据" + deptId);
        Dept dept = deptService.getInfo(deptId);
        return success(dept);
    }

    @PutMapping("/depts")
    public Result updateInfo(@RequestBody Dept dept){
        System.out.println("修改部门信息" + dept);
        deptService.update(dept);
        return success();
    }
}
